package cn.kgc.kgde.bookstore.service.impl;

import cn.kgc.kgde.bookstore.dto.CartDTO;
import cn.kgc.kgde.bookstore.entity.*;
import cn.kgc.kgde.bookstore.mapper.CartMapper;
import cn.kgc.kgde.bookstore.mapper.OrderDetailMapper;
import cn.kgc.kgde.bookstore.mapper.OrderMapper;
import cn.kgc.kgde.bookstore.service.CartService;
import cn.kgc.kgde.bookstore.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.ObjectError;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author myt
 * @since 2022-05-30
 */
@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;

    //查看订单 -- 后台 -- 查看所有 -- 条件 -- 根据订单状态、id、reciever查询
    @Override
    public IPage<Order> findOrder(Order order, Integer pageNum, Integer pageSize) {
        IPage<Order> page = new Page<>(pageNum, pageSize);
        //创建一个QueryWrapper的对象
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        //通过QueryWrapper设置条件
        if (order != null) {
            if (order.getOrderId() != null) {
                wrapper.like("orderId", order.getOrderId());
            }
            if (order.getState() != null && order.getState() != 0) {
                //根据订单状态进行查询
                wrapper.eq("state", order.getState());
            }
            if (order.getReciever() != null && order.getReciever() != "") {
                //根据 reciever 进行查询
                wrapper.like("reciever", order.getReciever());
            }
        }
        wrapper.orderByDesc("createTime");//默认根据时间降序
        IPage<Order> orderIPage = orderMapper.selectPage(page, wrapper);
        return orderIPage;
    }

    //修改订单状态 - 后台 操作订单状态信息
    @Override
    public void modify(Order order) {
        orderMapper.updateById(order);
    }

    //修改订单状态 - 后台 更改订单信息
    @Override
    public void modify(String orderId, Integer state) {
        Order order = new Order();
        order.setOrderId(orderId);
        order.setState( state );
        this.orderMapper.updateById(order);
    }


    //生成订单 - 后台 -购物车结算生成订单
    @Resource
    private CartMapper cartMapper;
    @Override
    public List<CartDTO> cartDTOList(Order order,String userCode){
        User user=new User();
        user.setUserCode(userCode);
        order.setUserCode(user.getUserCode());
        MPJLambdaWrapper<CartDTO> wrapper = MPJWrappers.<CartDTO>lambdaJoin()
                .select(Cartitem::getBookId,Cartitem::getQuantity,Cartitem::getCost)
                .select(Book::getBookName,Book::getPrice)
                .innerJoin(Cartitem.class,Cartitem::getCartId,Cart::getCartId)
                .innerJoin(Book.class,Book::getId,Cartitem::getBookId)
                .eq(Cart::getUserCode,order.getUserCode());
        List<CartDTO> cartDTOList=cartMapper.selectJoinList(CartDTO.class,wrapper);
        for (CartDTO record : cartDTOList) {
            record.setCost(record.getPrice()* record.getQuantity());
            System.out.println("record = " + record);
        }

        return cartDTOList;
    }

    @Override
    public Order add(Order order, List<CartDTO> carts) {
//        System.out.println("order = " + order);
        order.setOrderId(this.getOrderIdByTime());
        System.out.println("order = " + order);
        this.orderMapper.insert(order);
        System.out.println(order+"insert order");
        OrderDetail orderDetail = new OrderDetail();
        for (CartDTO cart : carts) {
            orderDetail.setOrderId(order.getOrderId());
            orderDetail.setBookId(cart.getBookId());
            orderDetail.setBookName(cart.getBookName());
            orderDetail.setBookPrice(cart.getPrice());
            orderDetail.setQuantity(cart.getQuantity());
            orderDetail.setCost(cart.getCost());
            this.orderDetailMapper.insert(orderDetail);
        }
        return order;
    }
    //生成订单编号
    @Override
    public  String getOrderIdByTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate = sdf.format(new Date());
        String result = "";
        String first = "DD";
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            result += random.nextInt(10);
        }
        return first + newDate + result;
    }
}

